package br.edu.ufcg.lsd.wbis.control.equipment;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;

import br.edu.ufcg.lsd.wbis.DatabaseTestCase;
import br.edu.ufcg.lsd.wbis.control.user.User;

public class HibernateEquipmentDAOTest extends DatabaseTestCase {

	EquipmentDAO equipmentDAO;
	
	@Override
	public void setUp() throws Exception {
		super.setUp();
		equipmentDAO = new HibernateEquipmentDAO();
	}
	
	@Override
	public void tearDown() throws Exception {
		super.tearDown();
		equipmentDAO = null;
	}
	
	@Test
	public void testAddRouter(){
		
		// Check if the database is empty
		session.beginTransaction();
		List<User> allRouters = session.createQuery("from " + Router.class.getCanonicalName()).list();
		assertTrue(allRouters.isEmpty());
		session.getTransaction().commit();
		
		// Add hub
		Router hub = new Router("ABC","Cisco","884321",getDate(), "Ak32",
								"sapupara",null, true, RouterType.HUB, "", 3);
		equipmentDAO.addEquipment(hub);
		
		// Check if hub is on the DB
		session.beginTransaction();
		Router returnedRouter = (Router) session.createQuery("from " + Router.class.getCanonicalName() + " where routerType=\'" + RouterType.HUB + "\'").uniqueResult();
		assertEquals(hub, returnedRouter);
		session.getTransaction().commit();
		
		// Add kvm switch
		Router kvm = new Router("DEF","System3","884302",getDate(), "Ak47-SSP",
				"havana",null,false, RouterType.SWITCH_KVM, "10.10.10.2/24", 3); 
		equipmentDAO.addEquipment(kvm);
		
		// Check if kvm is on the DB
		session.beginTransaction();
		returnedRouter = (Router) session.createQuery("from " + Router.class.getCanonicalName() + " where routerType=\'" + RouterType.SWITCH_KVM + "\'").uniqueResult();
		assertEquals(kvm, returnedRouter);
		session.getTransaction().commit();
		
		// Add network switch
		Router network = new Router("JIF","NetSistemas","39281",getDate(), "NETS1",
				"havana",null, false, RouterType.SWITCH_NETWORK, "", 8); 
		equipmentDAO.addEquipment(network);
		
		// Check if network switch is on the DB
		session.beginTransaction();
		returnedRouter = (Router) session.createQuery("from " + Router.class.getCanonicalName() + " where routerType=\'" + RouterType.SWITCH_NETWORK + "\'").uniqueResult();
		assertEquals(network, returnedRouter);
		session.getTransaction().commit();
		
		// Check if all the routers are on the DB
		session.beginTransaction();
		allRouters = session.createQuery("from " + Router.class.getCanonicalName()).list();
		assertEquals(3, allRouters.size());
		assertTrue(allRouters.contains(hub));
		assertTrue(allRouters.contains(kvm));
		assertTrue(allRouters.contains(network));
		session.getTransaction().commit();
	}
	
	@Test
	public void testInheritance(){
		
		// Check if the database is empty
		session.beginTransaction();
		List<NoBreak> allNoBreaks = equipmentDAO.getEquipments(NoBreak.class);
		assertTrue(allNoBreaks.isEmpty());
		session.getTransaction().commit();
		
		// Add hub
		NoBreak noBreak = new NoBreak("PW32","PowerSolutions","Jsi3298",getDate(),"SP12", "sapupara", null, true, 3);
		equipmentDAO.addEquipment(noBreak);
		
		// Check if nobreak is on the DB
		NoBreak returnedNoBreak = equipmentDAO.getEquipment(NoBreak.class, "PW32");
		assertEquals(noBreak, returnedNoBreak);
		
		// Remove nobreak from DB
		Equipment noBreakAsEquipment = equipmentDAO.getEquipment(Equipment.class, "PW32");
		equipmentDAO.removeEquipment(noBreakAsEquipment);
		
		// Check if nobreak is NOT on the DB
		returnedNoBreak = equipmentDAO.getEquipment(NoBreak.class, "PW32");
		assertNull(returnedNoBreak);
		noBreakAsEquipment = equipmentDAO.getEquipment(Equipment.class, "PW32");
		assertNull(noBreakAsEquipment);
		
		Router networkSwitch = new Router("AJD","CISCO","anje3219",getDate(),"JK432","havana",null,true,RouterType.SWITCH_NETWORK,"",6);
		networkSwitch.addIPAdress("150.165.90.11");
		networkSwitch.addMacAddress("30:AE:27:8A");
		networkSwitch.addHostName("bacalharte");
		networkSwitch.addHostName("seghidro");
		equipmentDAO.addEquipment(networkSwitch);
		
		equipmentDAO.removeEquipment(networkSwitch);
		
		assertNull(equipmentDAO.getEquipment(Equipment.class, "AJD"));
		
		Router otherSwitch = new Router("AJD","CISCO","anje3219",getDate(),"JK432","havana",null,true,RouterType.SWITCH_NETWORK,"",6);
		equipmentDAO.addEquipment(otherSwitch);
		
		otherSwitch = equipmentDAO.getEquipment(Router.class, "AJD");
		assertNotSame(networkSwitch, otherSwitch);
		
		otherSwitch.addIPAdress("150.165.90.11");
		otherSwitch.addMacAddress("30:AE:27:8A");
		otherSwitch.addHostName("bacalharte");
		otherSwitch.addHostName("seghidro");
		equipmentDAO.updateEquipment(otherSwitch);
		
		assertEquals(networkSwitch, equipmentDAO.getEquipment(Equipment.class, "AJD"));
	}
}
